package com.sitech.ibnms.c3p.task;

import com.sitech.ibnms.c3p.domain.C3pServerLogTask;
import com.sitech.ibnms.c3p.domain.C3pTask;
import com.sitech.ibnms.c3p.domain.C3pTaskNode;
import com.sitech.ibnms.c3p.mappers.C3pServerLogTaskMapper;
import com.sitech.ibnms.c3p.mappers.C3pTaskMapper;
import com.sitech.ibnms.c3p.mappers.C3pTaskNodeMapper;
import com.sitech.ismp.workflow.dao.ProcessInstanceDAO;
import com.sitech.ismp.workflow.domain.ProcessInstance;
import com.sitech.ismp.workflow.domain.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import static org.apache.commons.lang3.StringUtils.defaultString;

/**
 * Created by liujhc on 10/10.
 */
@Component
public class C3pServerLogTaskServiceImpl implements C3pServerLogTaskService {

    private static final int LOG_CONTENT_LENGTH = 2000;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private C3pTaskMapper c3pTaskMapper;
    @Autowired
    private C3pTaskNodeMapper nodeMapper;
    @Autowired
    private C3pServerLogTaskMapper logTaskMapper;
    @Autowired
    private ProcessInstanceDAO processInstanceDAO;

    @Override
    public void writeTaskNodeLog(Task task, String logPattern) {
        C3pTaskNode node = nodeMapper.queryByProcessIdAndActivityId(task.getProcessId(), task.getCurrActId());
        Assert.notNull(node, String.format("Cannot find C3pTaskNode by [processId=%s, activityId=%s]", task.getProcessId(), task.getCurrActId()));
        ProcessInstance processInstance = processInstanceDAO.queryProcessInstanceInfoBySerialNo(task.getSerialNo());
        String logContent = trimLength(logPattern.replaceAll("\\$\\{nodeName}", defaultString(node.getName())));
        logger.debug("[serialNo={}, activityId={}] writeTaskNodeLog: {}", task.getSerialNo(), task.getCurrActId(), logContent);
        logTaskMapper.insert(new C3pServerLogTask(node.getC3pTaskId(), processInstance, task, logContent));
    }

    @Override
    public void writeTaskLog(Task task, String logPattern) {
        C3pTask c3pTask = c3pTaskMapper.queryBySerialNo(task.getSerialNo());
        Assert.notNull(c3pTask, String.format("Cannot find C3pTask by [serialNo=%s]", task.getSerialNo()));
        ProcessInstance processInstance = processInstanceDAO.queryProcessInstanceInfoBySerialNo(task.getSerialNo());
        String logContent = trimLength(logPattern.replaceAll("\\$\\{taskName}", defaultString(c3pTask.getTaskName())));
        logger.debug("[serialNo={}, activityId={}] writeTaskLog: {}", task.getSerialNo(), task.getCurrActId(), logContent);
        logTaskMapper.insert(new C3pServerLogTask(c3pTask.getTaskId(), processInstance, task, logContent));
    }

    @Override
    public void writeTaskLog(int processId, int serialNo, String logPattern) {
        Task task = new Task();
        task.setProcessId(processId);
        task.setSerialNo(serialNo);
        writeTaskLog(task, logPattern);
    }

    private String trimLength(String logContent) {
        if (logContent.length() > LOG_CONTENT_LENGTH) {
            return logContent.substring(0, LOG_CONTENT_LENGTH - 5) + " ...";
        } else {
            return logContent;
        }
    }
}
